package com.sdp.wx.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_wz_arrival.entity.WxWzArrival;
import com.sdp.wx.db.wx_wz_arrival.service.IWxWzArrivalService;
import com.sdp.wx.db.wx_wz_arrival_detail.entity.WxWzArrivalDetail;
import com.sdp.wx.handleImpl.WzArrivalHandleImpl;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-25
 */
@RestController
@RequestMapping("/WxWzArrivalController")
public class WxWzArrivalController {

	@Autowired
	private Query query;
	
	@Autowired
	private IWxWzArrivalService iWxWzArrivalService;
	
	@Autowired
	private WzArrivalHandleImpl wzArrivalHandleImpl;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxWzArrivalService, WxWzArrival.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody JSONObject obj) {
		if(obj==null || obj.get("formData")==null || obj.get("wzList")==null){
			return Result.error("到货信息不存在");
		}
		List<WxWzArrivalDetail> wzList = JSONArray.parseArray(JSON.toJSONString(obj.get("wzList")), WxWzArrivalDetail.class);
		WxWzArrival wxWzArrival = JSONObject.parseObject(JSON.toJSONString(obj.get("formData")), WxWzArrival.class);
		BusinessContext businessContext = new BusinessContext();
		businessContext.put(WxWzArrival.class, wxWzArrival);
		businessContext.put(WxWzArrivalDetail.class, wzList);
		wzArrivalHandleImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok(wxWzArrival);
		}else {
			return Result.error(businessContext.getMessage());
		}
	}
}

